﻿Imports System.Data.SqlTypes
Imports Infragistics.Win
Imports System.Configuration

Public Class AddEditFund

#Region "Declare"
    Private _FundId As Integer
    Private _IsLoading As Boolean = False
    Private _IsCurrentlySaved As Boolean = False
    Private _SaveProcess As Boolean = False
    Private _EntID As String = String.Empty
    Public Event AddParticularFundInGrid(ByVal FundId As Integer)
    Public Event CloseUserWindow(ByVal FundId As Integer)
    Public _LinkedData As DataTable = Nothing
    Private _IsDetailChanged As Boolean = True
    Private _IsInvFundLinked As Boolean = False
    Private _PerDataTable As DataTable = CreateDT()
    Public CheckValueChange = False
    Dim isDecimal As Boolean = False
    Private mstrScreenName As String = "Add Edit Fund"
    '----------------------------------------------------------------------------------------------
    Public mdtNewFundDetails As DataTable = Nothing
    Public mlstFundDetails As List(Of AddNewInvestor)
    Public mintProjectID As Integer = 0
    Public mintRowNumber As Integer
    Public mintInvestorID As Integer
    'Public mintFundID As Integer
    Public mstrInvestorName As String
    Public mblnIsEditInDataHub As Boolean = False
    Public mblnCancelDataHub As Boolean = False

    '----------------------------------------------------------------------------------------------

#End Region

#Region "Properties"

    Public Property FundId() As Integer

        Get
            Return _FundId
        End Get

        Set(ByVal value As Integer)
            _FundId = value
        End Set

    End Property

#End Region

#Region "Constructor"

    Public Sub New()
        InitializeComponent()
        GetValidatedControls()
        DisableRightClickInTextBox()
        SetConfiguration()
    End Sub

#End Region

#Region "Form Events"

    Private Sub AddEditFund_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        '_Thread = Nothing
        For lmFomrs As Integer = 0 To Application.OpenForms.Count - 1
            If Application.OpenForms(lmFomrs).Name = "FundManagement" Then
                RaiseEvent CloseUserWindow(FundId)
                RemoveHandler CloseUserWindow, AddressOf CType(Application.OpenForms(lmFomrs), FundManagement).CloseUserWindow
                RemoveHandler AddParticularFundInGrid, AddressOf CType(Application.OpenForms(lmFomrs), FundManagement).AddParticularFundInGrid
            End If
        Next
        'tbcInvestor = Nothing
    End Sub

    Private Sub AddEditFund_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If _IsCurrentlySaved = False Then
            If (FundId <> 0) Then
                If (CheckRecordExistence("fund", FundId) = False) Then
                    MessageBox.Show("Record you are trying to modify/delete either does not exists or deleted by some other user", m_strMsgBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Exit Sub
                End If
                If (_IsCurrentlySaved) Then
                    If (MessageBox.Show(MessageManager.GetMessage("FILLFORM"), m_strMsgBoxCaption, MessageBoxButtons.OKCancel, MessageBoxIcon.Information) = Windows.Forms.DialogResult.OK) Then
                        e.Cancel = True
                        Exit Sub
                    End If
                End If
                If CheckContentsChange() Or _IsInvFundLinked Then
                    If (MessageBox.Show("Do you wish to save?", mstrMsgBoxCaption, MessageBoxButtons.OKCancel, MessageBoxIcon.Information) = Windows.Forms.DialogResult.OK) Then
                        If IsValid() Then
                            e.Cancel = True
                            Exit Sub
                        End If
                        If IsEmptyControl() Then
                            e.Cancel = True
                            Exit Sub
                        Else
                            If InsertUpdateFund() = False Then
                                e.Cancel = True
                                Exit Sub
                            End If
                        End If

                    End If
                End If
            Else
                If CheckChanges() Or _IsInvFundLinked Then
                    If (MessageBox.Show("Do you want to save the fund?", mstrMsgBoxCaption, MessageBoxButtons.OKCancel, MessageBoxIcon.Information) = Windows.Forms.DialogResult.OK) Then
                        If IsValid() Then
                            e.Cancel = True
                            Exit Sub
                        End If
                        If IsEmptyControl() Then
                            e.Cancel = True
                            Exit Sub
                        Else
                            If InsertUpdateFund() = False Then
                                e.Cancel = True
                                Exit Sub
                            End If
                        End If
                    End If
                End If
            End If
        End If
    End Sub
    Private Function IsEmptyControl() As Boolean
        If txtBeta.Text = String.Empty Or txtDYield.Text = String.Empty Or txtPERatio.Text = String.Empty Or txtPMomentum.Text = String.Empty Or _
            txtPRRatio.Text = String.Empty Or txtPValue.Text = String.Empty Or txtRelativeStrength.Text = String.Empty Or txtSalesGrowth.Text = String.Empty Then
            Return True
        End If
        Return False

    End Function
    Private Sub AddEditFund_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dtData As New DataTable
        Dim dRow As DataRow
        FillControls()
        If mblnCancelDataHub Then
            Exit Sub
        End If
        If (FundId <> 0) Then
            GetInvestorByFundID(FundId)
        Else
            SetPropertiesNewFund()
            dtData = CreateDT()
            If mintInvestorID <> 0 And mstrInvestorName <> String.Empty Then
                dRow = dtData.NewRow()
                dRow("InvestorID") = mintInvestorID
                dRow("Investor Name") = mstrInvestorName
                dtData.Rows.Add(dRow)
            End If
            grdInvestors.DataSource = dtData
            HideDefaultColumn()
            'If mintFundID <> 0 Then
            '    GetInvestorByFundID(mintFundID)
            'End If
        End If
        Me.grdInvestors.DisplayLayout.Override.CellClickAction = Infragistics.Win.UltraWinGrid.CellClickAction.CellSelect
        rdActive.Focus()
    End Sub

#End Region

#Region "Combo Events"

    Private Sub cmbType_DropdownItemSelected(ByVal sender As Object, ByVal e As Crystal.CustomControls.ComboToolTip.DropdownItemSelectedEventArgs) Handles cmbFundType.DropdownItemSelected
        If (e.SelectedItem < 0 Or e.Scrolled) Then
            ToolTip1.Hide(cmbFundType)
        Else
            ToolTip1.Hide(cmbFundType)
            Dim textWidth As Integer = cmbFundType.Items(e.SelectedItem)(0).ToString().Length
            If textWidth > 22 Then
                ToolTip1.Show(cmbFundType.Items(e.SelectedItem)(0).ToString(), cmbFundType, e.Bounds.Location)
            End If
        End If
    End Sub

    Private Sub cmbCountry_DropdownItemSelected(ByVal sender As Object, ByVal e As Crystal.CustomControls.ComboToolTip.DropdownItemSelectedEventArgs) Handles cmbCountry.DropdownItemSelected
        If (e.SelectedItem < 0 Or e.Scrolled) Then
            ToolTip1.Hide(cmbCountry)
        Else
            ToolTip1.Hide(cmbCountry)
            Dim textWidth As Integer = cmbCountry.Items(e.SelectedItem)(0).ToString().Length
            If textWidth > 22 Then
                ToolTip1.Show(cmbCountry.Items(e.SelectedItem)(0).ToString(), cmbCountry, e.Bounds.Location)
            End If
        End If
    End Sub

    Private Sub cmbType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbFundType.SelectedIndexChanged

        Dim obj As New BusinessLayer.cAddModifyFund
        Dim dr As DataRow = Nothing
        Dim dt As New DataTable
        Dim idr As IDataReader = Nothing
        Dim dsReturn As DataSet = Nothing
        Dim Id As String = String.Empty
        Dim InvestorType As String = String.Empty
        Try
            If _IsLoading = False Then
                If cmbFundType.SelectedValue = m_strNewIDValue Then
                    Dim objAddValue As New CrystalCommon.View.AddValue(enmEntityType.Source.ToString(), LKPMaster.FNDTP, "Fund Type")
                    objAddValue.ShowDialog()
                    Id = objAddValue.ID
                    If Id <> String.Empty Then
                        dsReturn = obj.GetFundByID(Id)
                        If dsReturn IsNot Nothing Then
                            InvestorType = dsReturn.Tables(0).Rows(0)("FundType").ToString()
                            Id = dsReturn.Tables(0).Rows(0)("FundTypeID").ToString()
                            Dim table As DataTable = cmbFundType.DataSource
                            If table.Columns.Contains("FundTypeID") Then
                                Dim FilterQuery As String
                                Dim Excel() As DataRow
                                FilterQuery = "FundTypeID ='" + Trim(Id).Replace("'", "''") + "'"
                                Excel = table.Select(FilterQuery)
                                If Excel.Length = 0 Then
                                    If DropDownAtSecond(table, cmbFundType.ValueMember, cmbFundType.DisplayMember, Id, InvestorType) = False Then
                                        mdlCommon.ShowMessage("Error in accessing Fund Type drop down.", MessageBoxIcons.Error)
                                        Exit Sub
                                    End If
                                End If
                                cmbFundType.SelectedValue = Id
                            End If
                        End If
                    Else
                        cmbFundType.SelectedValue = m_strSelectIDValue
                    End If
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Dim Logmessage As New System.Text.StringBuilder
            Logmessage.Append("There is error in Fundtype change event of form addeditfund.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
            Logmessage = Nothing
        Finally
            obj = Nothing
            dr = Nothing
            dt = Nothing
            idr = Nothing
            dsReturn = Nothing
            Id = String.Empty
            InvestorType = String.Empty
        End Try
    End Sub

    Private Sub cmbStyle_DropdownItemSelected(ByVal sender As Object, ByVal e As Crystal.CustomControls.ComboToolTip.DropdownItemSelectedEventArgs) Handles cmbStyle.DropdownItemSelected
        If (e.SelectedItem < 0 Or e.Scrolled) Then
            ToolTip1.Hide(cmbStyle)
        Else
            ToolTip1.Hide(cmbStyle)
            Dim textWidth As Integer = cmbStyle.Items(e.SelectedItem)(0).ToString().Length
            If textWidth > 30 Then
                ToolTip1.Show(cmbStyle.Items(e.SelectedItem)(0).ToString(), cmbStyle, e.Bounds.Location)
            End If
        End If
    End Sub

    Private Sub cmbStyle_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbStyle.SelectedIndexChanged
        Dim dsReturn As DataSet = Nothing
        Dim data As DataTable = Nothing
        Dim Id As String = String.Empty
        Dim Style As String = String.Empty
        Try
            If _IsLoading = False Then
                If cmbStyle.SelectedValue = m_strNewIDValue Then
                    Dim objAddValue As New CrystalCommon.View.AddValue(enmEntityType.Source.ToString(), LKPMaster.STYLE, "Style")
                    objAddValue.ShowDialog()
                    Id = objAddValue.ID
                    If Id <> String.Empty Then
                        Dim obj As New BusinessLayer.cAddModifyInvestor
                        dsReturn = obj.GetStyleByID(Id)
                        If dsReturn IsNot Nothing Then
                            Style = dsReturn.Tables(0).Rows(0)("Style").ToString()
                            Id = dsReturn.Tables(0).Rows(0)("StyleID").ToString()
                            Dim table As DataTable = cmbStyle.DataSource
                            If table.Columns.Contains("StyleID") Then
                                Dim FilterQuery As String
                                Dim Excel() As DataRow
                                FilterQuery = "StyleID ='" + Trim(Id).Replace("'", "''") + "'"
                                Excel = table.Select(FilterQuery)
                                If Excel.Length = 0 Then
                                    If DropDownAtSecond(table, cmbStyle.ValueMember, cmbStyle.DisplayMember, Id, Style) = False Then
                                        mdlCommon.ShowMessage("Error in accessing Style drop down.", MessageBoxIcons.Error)
                                        Exit Sub
                                    End If
                                End If
                                cmbStyle.SelectedValue = Id
                            End If
                        End If
                    Else
                        cmbStyle.SelectedValue = m_strSelectIDValue
                    End If
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Dim Logmessage As New System.Text.StringBuilder
            Logmessage.Append("There is error in style change event of form addeditfund.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
            Logmessage = Nothing
        Finally
            Id = String.Empty
            Style = String.Empty
        End Try
    End Sub

    Private Sub cmbCountry_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbCountry.SelectedIndexChanged
        Dim obj As New BusinessLayer.cAddModifyInvestor
        Dim Id As String = String.Empty
        Dim Country As String = String.Empty
        Dim dsReturn As DataSet = Nothing
        Try
            If _IsLoading = False Then
                If cmbCountry.SelectedValue = m_strNewIDValue Then
                    Dim objAddValue As New CrystalCommon.View.AddValue(enmEntityType.Source.ToString(), LKPMaster.CNTRY, "Country")
                    objAddValue.ShowDialog()
                    Id = objAddValue.ID
                    If Id <> String.Empty Then
                        dsReturn = obj.GetCountryByID(Id)
                        If dsReturn IsNot Nothing Then
                            Country = dsReturn.Tables(0).Rows(0)("CountryName").ToString()
                            Id = dsReturn.Tables(0).Rows(0)("CountryID").ToString()
                            Dim table As DataTable = cmbCountry.DataSource
                            If table.Columns.Contains("CountryID") Then
                                Dim FilterQuery As String
                                Dim Excel() As DataRow
                                FilterQuery = "CountryID ='" + Trim(Id).Replace("'", "''") + "'"
                                Excel = table.Select(FilterQuery)
                                If Excel.Length = 0 Then
                                    If DropDownAtSecond(table, cmbCountry.ValueMember, cmbCountry.DisplayMember, Id, Country) = False Then
                                        mdlCommon.ShowMessage("Error in accessing Country drop down.", MessageBoxIcons.Error)
                                        Exit Sub
                                    End If

                                End If
                                cmbCountry.SelectedValue = Id
                            End If

                        End If
                    Else
                        cmbCountry.SelectedValue = m_strSelectIDValue
                    End If
                End If
            End If

        Catch ex As Exception
            ShowMessageInfo(ex.ToString)
            Dim Logmessage As New System.Text.StringBuilder
            Logmessage.Append("There is error in country selected index of form addeditfund.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
            Logmessage = Nothing
        Finally
            obj = Nothing
            Id = String.Empty
            Country = String.Empty
            dsReturn = Nothing
        End Try
    End Sub

#End Region

#Region "Method"
    Private Sub SetConfiguration()
        If BLLmdlCommon.strConfiguration Is Nothing Then BLLmdlCommon.strConfiguration = CommonCLL.mstrConnectionstring 'ConfigurationManager.AppSettings("SqlServer")
    End Sub

    Private Sub DisableRightClickInTextBox()
        DisableRightClickInfTextEditor(txtFundName)
        DisableRightClickInfTextEditor(txtBeta)
        DisableRightClickInfTextEditor(txtDYield)
        DisableRightClickInfTextEditor(txtPERatio)
        DisableRightClickInfTextEditor(txtPMomentum)
        DisableRightClickInfTextEditor(txtPRRatio)
        DisableRightClickInfTextEditor(txtPValue)
        DisableRightClickInfTextEditor(txtRelativeStrength)
        DisableRightClickInfTextEditor(txtSalesGrowth)
    End Sub

    Private Sub HideDefaultColumn()
        With grdInvestors
            If .DisplayLayout.Bands(0).Columns.Exists("InvestorID") Then
                .DisplayLayout.Bands(0).Columns("InvestorID").Hidden = True
            End If
        End With
    End Sub
    Private Sub ShowMessageInfo(ByVal message As String)
        MessageBox.Show(message, "Crystal", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Function CreateDT() As DataTable
        Dim clone As DataTable = Nothing
        Try
            clone = New DataTable
            clone.Columns.Add("InvestorID", GetType(Integer))
            clone.Columns.Add("Investor Name", GetType(String))

            Return clone
        Catch ex As Exception
            If (Not Me.IsDisposed) Then
                MessageBox.Show(ex.Message)
            End If
            Return Nothing
        Finally
            clone = Nothing
        End Try
    End Function

    Public Sub GetInvestorByFundID(ByVal FundId As Integer)
        Dim idr As IDataReader = Nothing
        Dim dtInvestor As New DataTable
        Try
            idr = cAddModifyFund.GetInvestorByFundID(FundId)
            dtInvestor.Load(idr)
            grdInvestors.DataSource = dtInvestor
            If grdInvestors.Rows.Count = 0 Then
                grdInvestors.DataSource = CreateDT()
            End If
            With grdInvestors
                If .DisplayLayout.Bands(0).Columns.Exists("InvestorID") Then
                    .DisplayLayout.Bands(0).Columns("InvestorID").Hidden = True
                End If
            End With
        Catch ex As Exception
            ShowMessageInfo(ex.ToString)
            Dim Logmessage As New System.Text.StringBuilder
            Logmessage.Append("There is error in GetInvestorByFundID of form addeditfund.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
            Logmessage = Nothing
        Finally
            idr = Nothing
            dtInvestor = Nothing
        End Try
    End Sub

    Private Function IsValid() As Boolean
        If Not (Me.ValidateChildren()) Then
            ValidationManager.IsButtonClick = False
            'FocusOnEmptyControl()
            Return True
        End If
        ValidationManager.IsButtonClick = False
        Return False
    End Function
    Private Function CheckChanges() As Boolean
        If txtFundName.Text <> String.Empty Or cmbCountry.SelectedValue <> Nothing Or cmbFundType.SelectedValue <> Nothing Or cmbStyle.SelectedValue <> Nothing _
           Or cmbTurnoverLabel.SelectedValue <> Nothing Or txtBeta.Value IsNot Nothing Or txtDYield.Value IsNot Nothing Or txtPERatio.Value IsNot Nothing _
           Or txtPMomentum.Value IsNot Nothing Or txtPRRatio.Value IsNot Nothing Or txtPValue.Value IsNot Nothing Or txtRelativeStrength.Value IsNot Nothing _
           Or txtSalesGrowth.Value IsNot Nothing Then
            Return True
        End If
    End Function

    Private Function InsertUpdateFund() As Boolean
        Dim ds As DataSet = Nothing
        Dim objInv As cAddModifyFund = Nothing
        Dim UltimateParentId As Integer = 0
        Dim newid As Boolean = False
        Dim Dt As DataSet = Nothing
        Dim lclsValidateInvestor As ValidateInvestor = Nothing
        Try
            If _IsDetailChanged = True Then
                If (FundId = 0) Then
                    If txtFundName.Text = String.Empty Then
                        Return False
                    End If
                    Dt = cAddModifyFund.GetFundNameCount(txtFundName.Text)
                    If Convert.ToInt32(Dt.Tables(0).Rows(0)(0).ToString()) <> 0 Then
                        ShowMessage("Fund name already exist.")
                        Return False
                    End If
                Else
                    Dt = cAddModifyFund.GetFundNameCount(txtFundName.Text)
                    If Dt.Tables(1).Rows.Count > 0 Then
                        If lblFundID.Text <> Dt.Tables(1).Rows(0)(0).ToString() Then
                            ShowMessage("Fund name already exist.")
                            Return False
                        End If
                    End If
                End If
                _SaveProcess = True
                Dim objFund As New cAddModifyFund
                objFund = New BusinessLayer.cAddModifyFund
                If FundId > 0 Then
                    objFund.FundId = FundId
                End If
                objFund.FundName = txtFundName.Text.Trim()
                If (cmbStyle.Text = String.Empty) Then
                    Dim d As DataRowView = cmbStyle.SelectedItem
                    For ij As Integer = 0 To cmbStyle.Items.Count - 1
                        d = cmbStyle.Items(ij)
                        If (d.Row.Item("style").ToString().ToLower = "na") Then
                            cmbStyle.SelectedIndex = ij
                            objFund.StyleId = d.Row.Item("styleid")
                            Exit For
                        End If
                    Next
                Else
                    objFund.StyleId = cmbStyle.SelectedValue
                End If
                objFund.FundTypeId = cmbFundType.SelectedValue
                objFund.TurnoverId = cmbTurnoverLabel.SelectedValue
                objFund.CountryId = cmbCountry.SelectedValue
                objFund.LastReportDate = dtLastReport.Value
                If txtPRRatio.Text.Length > 0 Then
                    objFund.PBratio = txtPRRatio.Value
                End If
                If txtPERatio.Text.Length > 0 Then
                    objFund.PEratio = txtPERatio.Value
                End If
                If txtPValue.Text.Length > 0 Then
                    objFund.PortFolio = txtPValue.Value
                End If
                If txtPMomentum.Text.Length > 0 Then
                    objFund.PriceMomentum = txtPMomentum.Value
                End If
                If txtDYield.Text.Length > 0 Then
                    objFund.DividendYield = txtDYield.Value
                End If
                If txtSalesGrowth.Text.Length > 0 Then
                    objFund.SalesGrowth = txtSalesGrowth.Value
                End If
                If txtRelativeStrength.Text.Length > 0 Then
                    objFund.RelativeStrength = txtRelativeStrength.Value
                End If
                If txtBeta.Text.Length > 0 Then
                    objFund.Beta = txtBeta.Value
                End If
                Dim val As Integer
                val = Convert.ToInt16(System.Configuration.ConfigurationManager.AppSettings("DefaultSourceTypeInvestor").ToString())
                objFund.IsManaged = False
                objFund.CreatedDate = DateTime.Now.ToString
                objFund.ModifyDate = DateTime.Now.ToString
                objFund.CreatedBy = val
                objFund.ModifyBy = val
                If (FundId = 0) Then
                    objFund.IsCIRFund = True
                Else
                    objFund.IsCIRFund = False
                End If

                If (FundId = 0) Then
                    newid = True
                Else

                End If
                Dim va As Integer
                If rdActive.Checked Then
                    va = EntityStatus.ACTVE
                ElseIf rdDelete.Checked Then
                    va = EntityStatus.DELTE
                ElseIf rdDormant.Checked Then
                    va = EntityStatus.DRMNT
                End If
                objFund.EStatusID = va
                If mintProjectID > 0 And Not mblnIsEditInDataHub Then
                    lclsValidateInvestor = New ValidateInvestor
                    lclsValidateInvestor.Text = TitleManager.GetTitle("ValidateFund")
                    ValidateNewFund()
                    lclsValidateInvestor.mdtNewInvestor = mdtNewFundDetails
                    lclsValidateInvestor.mintProjectID = mintProjectID
                    lclsValidateInvestor.ShowDialog()
                    If lclsValidateInvestor.mblnCheckSaved Then
                        If lclsValidateInvestor.mdtNewInvestor.Rows(0)("FndEntityID").ToString <> String.Empty Then
                            FundId = lclsValidateInvestor.mdtNewInvestor.Rows(0)("FundID")
                            InsertFundInvLinking()
                            Return True
                        End If
                    Else
                        Return False
                    End If
                End If
                ds = objFund.InsertUpdateFundDetails(FundId, mblnIsEditInDataHub, mintProjectID, lblFundID.Text)
                If ds IsNot Nothing Then
                    If FundId <> 0 Then
                        ShowMessage("Fund updated successfully.")
                    Else
                        ShowMessage("Fund saved successfully.")
                    End If
                    _IsCurrentlySaved = True
                    For i As Integer = 0 To ds.Tables(ds.Tables.Count - 1).Rows.Count - 1
                        If (Not ds.Tables(ds.Tables.Count - 1).Rows(i).Item(1) Is System.DBNull.Value And Not ds.Tables(ds.Tables.Count - 1).Rows(i).Item(1).ToString = "") Then
                            FundId = ds.Tables(ds.Tables.Count - 1).Rows(i).Item(0).ToString().Trim()
                            _EntID = ds.Tables(ds.Tables.Count - 1).Rows(i).Item(1).ToString().Trim()
                        End If
                    Next
                    RaiseEvent AddParticularFundInGrid(FundId)
                    If (Me.IsDisposed) Then Exit Function
                End If
            End If
            If FundId > 0 Then
                If (_LinkedData IsNot Nothing And _IsInvFundLinked = True) Or mintInvestorID > 0 Then
                    'If _LinkedData.Rows.Count > 0 Or mintInvestorID > 0 Then
                    InsertFundInvLinking()
                    RaiseEvent AddParticularFundInGrid(FundId)

                    If _IsDetailChanged = False Then
                        ShowMessage("Fund updated successfully.")
                    End If
                End If
                _IsInvFundLinked = False
                'End If
            End If

            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Dim Logmessage As New System.Text.StringBuilder
            Logmessage.Append("There is error in add update of form addeditfund.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
            Logmessage = Nothing
        Finally
            objInv = Nothing
            ds = Nothing
            _SaveProcess = False
        End Try
    End Function

    Private Sub ValidateNewFund()
        Dim larrInvestors() As String = {"Select", "FundID", "FndEntityID", "FundName", "Type", "Style", "Turnover", "City", "Country"}
        Dim ldrow As DataRow
        Try
            mdtNewFundDetails = New DataTable
            For i As Integer = 0 To larrInvestors.Length - 1
                If larrInvestors(i) = "Select" Then
                    mdtNewFundDetails.Columns.Add(larrInvestors(i), GetType(Boolean))
                Else
                    mdtNewFundDetails.Columns.Add(larrInvestors(i))
                End If
            Next
            ldrow = mdtNewFundDetails.NewRow()
            ldrow("Select") = True
            ldrow("FundID") = DBNull.Value
            ldrow("FndEntityID") = String.Empty
            ldrow("FundName") = txtFundName.Text
            ldrow("Type") = cmbFundType.Text
            ldrow("Style") = cmbStyle.Text
            ldrow("Turnover") = cmbTurnoverLabel.Text
            ldrow("Country") = cmbCountry.Text
            mdtNewFundDetails.Rows.Add(ldrow)
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, mintProjectID)
        Finally
            ldrow = Nothing
            larrInvestors = Nothing
        End Try
    End Sub

    Private Sub AddColumns()
        If _LinkedData Is Nothing And mintInvestorID > 0 Then
            _LinkedData = New DataTable
            _LinkedData.Columns.Add("InvestorID")
            _LinkedData.Columns.Add("Investor Name")
        End If
    End Sub

    Private Sub InsertFundInvLinking()
        Dim ldrow As DataRow
        Dim IdString As String = String.Empty
        If FundId > 0 Then
            If mintInvestorID > 0 Then
                If _LinkedData Is Nothing Then
                    AddColumns()
                End If
                ldrow = _LinkedData.NewRow()
                ldrow("InvestorID") = mintInvestorID
                ldrow("Investor Name") = mstrInvestorName
                _LinkedData.Rows.Add(ldrow) '.ImportRow(ldrow)
                _PerDataTable.ImportRow(ldrow)
            End If
            If _LinkedData IsNot Nothing Then
                IdString = GetXMlFromDataTable(_PerDataTable, {_LinkedData.Columns("Investor Name").ColumnName.ToString()})
                Dim obj As New cAddModifyFund
                If FundId > 0 Then
                    obj.InsertFundInvestorLinking(FundId, IdString)
                End If
                obj = Nothing
                _PerDataTable = Nothing
            End If
        End If
    End Sub
    Private Function CheckContentsChange() As Boolean
        Dim datatable As New DataTable
        Dim idr As IDataReader = Nothing
        Dim i As Integer = 0
        Try
            idr = cAddModifyFund.GetFundByFundID(FundId)
            datatable.Load(idr)
            ChangeDataReaderDataTable(datatable)
            If (datatable.Rows.Count = 0) Then
                Return False
            End If

            For i = 0 To datatable.Rows.Count - 1
                If (IsDBNull(datatable.Rows(i).Item("FundName")) = False) Then
                    If (datatable.Rows(i).Item("FundName").ToString().Trim() <> txtFundName.Text.Trim()) Then
                        Return True
                    End If
                Else
                    If (txtFundName.Text <> "") Then
                        Return True
                    End If
                End If

                If (IsDBNull(datatable.Rows(i).Item("FundTypeId")) = False) Then
                    Dim drv As DataRowView = cmbFundType.SelectedItem
                    If (drv.Row.Item("FundTypeId").ToString().Trim() <> datatable.Rows(i).Item("FundTypeId").ToString().Trim()) Then
                        If (cmbFundType.SelectedIndex <> m_strSelectIDValue) Then
                            Return True
                        End If
                        Dim res = (From p In CType(cmbFundType.DataSource, DataTable).AsEnumerable Where p.Item("FundTypeId").ToString = datatable.Rows(i).Item("InvestorTypeId").ToString()).SingleOrDefault
                        If (Not res Is Nothing) Then
                            Return True
                        End If
                    End If
                Else
                    If (cmbFundType.SelectedIndex <> 0) Then
                        Return True
                    End If
                End If


                If (IsDBNull(datatable.Rows(i).Item("StyleId")) = False) Then
                    Dim drv As DataRowView = cmbStyle.SelectedItem
                    Dim style As String = ""
                    If (drv.Row.Item("StyleId").ToString().Trim() <> datatable.Rows(i).Item("StyleId").ToString().Trim()) Then
                        Return True
                    End If
                Else
                    If (cmbStyle.SelectedIndex <> 0) Then
                        Return True
                    End If
                End If

                If (IsDBNull(datatable.Rows(i).Item("TurnoverId")) = False) Then
                    Dim drv As DataRowView = cmbTurnoverLabel.SelectedItem
                    If (drv.Row.Item("TurnoverId").ToString().Trim() = BLLmdlCommon.m_strSelectIDValue) Then
                        ' Return True
                        Dim dr() As DataRow = CType(cmbTurnoverLabel.DataSource, DataTable).Select("turnoverid=" + datatable.Rows(i).Item("TurnoverId").ToString())
                        If (dr.Length > 0) Then
                            If (dr(0)("Turnover").ToString().ToLower <> "na") Then
                                Return True
                            End If
                        End If

                    Else
                        Dim turnover As String = ""
                        Dim dr() As DataRow = CType(cmbTurnoverLabel.DataSource, DataTable).Select("turnoverid=" + datatable.Rows(i).Item("TurnoverId").ToString())
                        If (dr.Length > 0) Then
                            If (dr(0)("Turnover").ToString().ToLower = "na") Then
                                If (drv.Row.Item("TurnoverId") <> 0) Then
                                    Return True
                                End If
                            Else
                                If (drv.Row.Item("TurnoverId").ToString().Trim() <> datatable.Rows(i).Item("TurnoverId").ToString().Trim()) Then
                                    Return True
                                End If
                            End If
                        End If
                    End If
                Else
                    If (cmbTurnoverLabel.SelectedIndex <> 0) Then
                        Return True
                    End If
                End If


                If (IsDBNull(datatable.Rows(i).Item("CountryId")) = False) Then
                    Dim drv As DataRowView = cmbCountry.SelectedItem
                    Dim one As String = drv.Row.Item("CountryId").ToString().Trim()
                    Dim two As String = datatable.Rows(i).Item("CountryId").ToString().Trim()
                    If (one <> two) Then
                        Return True
                    End If
                Else
                    If (cmbCountry.SelectedIndex <> 0) Then
                        Return True
                    End If
                End If


                If (IsDBNull(datatable.Rows(i).Item("EStatusID")) = False) Then
                    Dim va As Integer = Convert.ToInt32(datatable.Rows(i).Item("EStatusID").ToString())
                    If va = 1 Then
                        If Not rdActive.Checked Then
                            Return True
                        End If

                    ElseIf va = 2 Then
                        If Not rdDormant.Checked Then
                            Return True
                        End If

                    ElseIf va = 4 Then
                        If Not rdDelete.Checked Then
                            Return True
                        End If
                    End If
                End If


                If (IsDBNull(datatable.Rows(i).Item("LastReportDate")) = False) Then
                    If dtLastReport.Value <> Nothing And dtLastReport.Value <> "1/1/2001" Then
                        If (dtLastReport.Value <> datatable.Rows(i).Item("LastReportDate")) Then
                            Return True
                        End If
                    End If

                Else
                    If (dtLastReport.Value IsNot Nothing) Then
                        Return True
                    End If
                End If
                If (IsDBNull(datatable.Rows(i).Item("Portfolio Value")) = False) Then
                    If (txtPValue.Value.ToString <> datatable.Rows(i).Item("Portfolio Value").ToString().Trim()) Then
                        Return True
                    End If
                Else
                    If (txtPValue.Text.Trim() <> "") Then
                        Return True
                    End If
                End If

                If (IsDBNull(datatable.Rows(i).Item("P/E Ratio")) = False) Then
                    If (txtPERatio.Value.ToString.Trim() <> datatable.Rows(i).Item("P/E Ratio").ToString().Trim()) Then
                        Return True
                    End If
                Else
                    If (txtPERatio.Text.Trim() <> "") Then
                        Return True
                    End If
                End If

                If (IsDBNull(datatable.Rows(i).Item("P/B Ratio")) = False) Then
                    If (txtPRRatio.Value.ToString.Trim() <> datatable.Rows(i).Item("P/B Ratio").ToString().Trim()) Then
                        Return True
                    End If
                Else
                    If (txtPRRatio.Text <> "") Then
                        Return True
                    End If
                End If

                If (IsDBNull(datatable.Rows(i).Item("Dividend Yield")) = False) Then
                    If (txtDYield.Value.ToString.Trim() <> datatable.Rows(i).Item("Dividend Yield").ToString().Trim()) Then
                        Return True
                    End If
                Else
                    If (txtDYield.Text.Trim() <> "") Then
                        Return True
                    End If
                End If

                If (IsDBNull(datatable.Rows(i).Item("Sales Growth")) = False) Then
                    If (txtSalesGrowth.Value.ToString.Trim() <> datatable.Rows(i).Item("Sales Growth").ToString().Trim()) Then
                        Return True
                    End If
                Else
                    If (txtSalesGrowth.Text.Trim() <> "") Then
                        Return True
                    End If
                End If
                If (IsDBNull(datatable.Rows(i).Item("Price Momentum")) = False) Then
                    If (txtPMomentum.Value.ToString.Trim() <> datatable.Rows(i).Item("Price Momentum").ToString().Trim()) Then
                        Return True
                    End If
                Else
                    If (txtPMomentum.Text.Trim() <> "") Then
                        Return True
                    End If
                End If
                If (IsDBNull(datatable.Rows(i).Item("Relative Strength")) = False) Then
                    If (txtRelativeStrength.Value.ToString.Trim() <> datatable.Rows(i).Item("Relative Strength").ToString().Trim()) Then
                        Return True
                    End If
                Else
                    If (txtRelativeStrength.Text.Trim() <> "") Then
                        Return True
                    End If
                End If
                If (IsDBNull(datatable.Rows(i).Item("Beta")) = False) Then
                    If (txtBeta.Value.ToString.Trim() <> datatable.Rows(i).Item("Beta").ToString().Trim()) Then
                        Return True
                    End If
                Else
                    If (txtBeta.Text.Trim() <> "") Then
                        Return True
                    End If
                End If

                Return False
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Dim Logmessage As New System.Text.StringBuilder
            Logmessage.Append("There is error in CheckContentChange of form AddEditfund.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
            Logmessage = Nothing
        Finally
            datatable = Nothing
            idr = Nothing

        End Try
    End Function

    Private Sub GetValidatedControls()

        ValidateControls(CType(pnlFund.Controls(0), Misc.UltraPanelClientArea).Controls, True)
        RangeValidator(txtPValue, "999999999999999", "0", lblPValue.Text)
        'RangeValidator(txtPERatio, "999999999999999", "0.000", lblPValue.Text)
        'RangeValidator(txtPRRatio, "999999999999999", "0.000", lblPValue.Text)
        'RangeValidator(txtDYield, "999999999999999", "0.000", lblPValue.Text)
        'RangeValidator(txtSalesGrowth, "999999999999999", "0.000", lblPValue.Text)
        'RangeValidator(txtPMomentum, "999999999999999", "0.000", lblPValue.Text)
        'RangeValidator(txtRelativeStrength, "999999999999999", "0.000", lblPValue.Text)
        'RangeValidator(txtBeta, "999999999999999", "0.000", lblPValue.Text)
        ' ApplyRegexValidation(txtPValue, ValidationManager.RegexType.Double, lblPValue.Text)
        ApplyRegexValidation(txtPRRatio, ValidationManager.RegexType.Double, lblPRRatio.Text)
        ApplyRegexValidation(txtPERatio, ValidationManager.RegexType.Double, lblPERatio.Text)
        ApplyRegexValidation(txtDYield, ValidationManager.RegexType.Double, lblDYield.Text)
        ApplyRegexValidation(txtSalesGrowth, ValidationManager.RegexType.Double, lblSales.Text)
        ApplyRegexValidation(txtPMomentum, ValidationManager.RegexType.Double, lblPMomentum.Text)
        ApplyRegexValidation(txtRelativeStrength, ValidationManager.RegexType.Double, lblRelative.Text)
        ApplyRegexValidation(txtBeta, ValidationManager.RegexType.Double, lblBeta.Text)
    End Sub

    Private Sub SetPropertiesNewFund()
        rdDelete.Enabled = False
        rdDormant.Enabled = False
        rdActive.Checked = True
        lblFundID.Visible = False
        lblFund.Visible = False
    End Sub

    Private Sub ClearAll()
        Try
            txtFundName.Text = ""
            cmbFundType.SelectedIndex = -1
            cmbCountry.SelectedIndex = -1
            cmbStyle.SelectedIndex = -1
            cmbTurnoverLabel.SelectedIndex = -1
            FundId = 0
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
#End Region

#Region "Places the row for ---select--- , ---others--- "

    Private Function DropDownAtFirst(ByRef dataTable As DataTable, ByVal idColumn As String, ByVal valueColumn As String, ByVal rowValue As Integer, ByVal rowText As String) As Boolean
        If dataTable Is Nothing Then Return True
        Dim drDataRow As DataRow
        drDataRow = dataTable.NewRow
        drDataRow.Item(idColumn) = rowValue
        drDataRow.Item(valueColumn) = rowText
        dataTable.Rows.InsertAt(drDataRow, 0)
        Return True
    End Function

    Private Function DropDownAtSecond(ByRef dataTable As DataTable, ByVal idColumn As String, ByVal valueColumn As String, ByVal rowValue As Integer, ByVal rowText As String) As Boolean
        If dataTable Is Nothing Then Return True
        Dim drDataRow As DataRow
        drDataRow = dataTable.NewRow
        drDataRow.Item(idColumn) = rowValue
        drDataRow.Item(valueColumn) = rowText
        dataTable.Rows.InsertAt(drDataRow, 1)
        Return True
    End Function

#End Region

#Region "Fill Controls"

    Private Sub FillControls()

        Try
            _IsLoading = True
            Call LoadFundDetails()
            If (FundId = 0) Then
                ClearAll()
                Return
            End If
            SetFundValue()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Dim Logmessage As New System.Text.StringBuilder
            Logmessage.Append("There is error in fillcontrol of form addeditfund.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
            Logmessage = Nothing
        Finally
            _IsLoading = False
            'datatable = Nothing
        End Try
    End Sub

#End Region

#Region "Load Fund Detail"

    Private Sub LoadFundDetails()
        Dim dr As DataRow
        Dim DataTable As DataTable
        Dim ds As New DataSet
        Dim IDR As IDataReader = Nothing
        Dim s(5) As String
        Try
            IDR = cAddModifyFund.GetOverViewDataSet()
            ds.Load(IDR, LoadOption.OverwriteChanges, s)
            mdlCommon.ChangeDataReaderDataSet(ds)

            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            cmbFundType.DisplayMember = "FundType"
            cmbFundType.ValueMember = "FundTypeID"
            DataTable = ds.Tables(0)
            cmbFundType.DataSource = DataTable
            If DropDownAtFirst(DataTable, cmbFundType.ValueMember, cmbFundType.DisplayMember, m_strNewIDValue, m_strNewTextValue) = False Then
                mdlCommon.ShowMessage("Error in accessing Investor Type drop down.", MessageBoxIcons.Error)
                Exit Sub
            End If

            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            'cmbStyle dupinder Singh
            cmbStyle.DisplayMember = "style"
            cmbStyle.ValueMember = "styleid"
            DataTable = ds.Tables(1)
            cmbStyle.DataSource = DataTable
            If DropDownAtFirst(DataTable, cmbStyle.ValueMember, cmbStyle.DisplayMember, m_strNewIDValue, m_strNewTextValue) = False Then
                mdlCommon.ShowMessage("Error in accessing Style drop down.", MessageBoxIcons.Error)
                Exit Sub
            End If

            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            'cmbTurnover
            cmbTurnoverLabel.DisplayMember = "Turnover"
            cmbTurnoverLabel.ValueMember = "TurnoverID"
            DataTable = ds.Tables(2)
            cmbTurnoverLabel.DataSource = DataTable
            If FundId <> 0 Then
                If DropDownAtFirst(DataTable, cmbTurnoverLabel.ValueMember, cmbTurnoverLabel.DisplayMember, 6, "NA") = False Then
                    mdlCommon.ShowMessage("Error in accessing Style drop down.", MessageBoxIcons.Error)
                    Exit Sub
                End If
            End If

            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            '''''''''cmbCountry'''''''''''''''''''''
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

            cmbCountry.DisplayMember = "CountryName"
            cmbCountry.ValueMember = "CountryID"
            DataTable = ds.Tables(3)
            cmbCountry.DataSource = DataTable
            If DropDownAtFirst(DataTable, cmbCountry.ValueMember, cmbCountry.DisplayMember, m_strNewIDValue, m_strNewTextValue) = False Then
                mdlCommon.ShowMessage("Error in accessing Country drop down.", MessageBoxIcons.Error)
                Exit Sub
            End If

            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            If FundId = 0 Then
                cmbCountry.SelectedValue = m_strSelectIDValue
                cmbFundType.SelectedValue = m_strSelectIDValue
                cmbStyle.SelectedValue = m_strSelectIDValue
                cmbTurnoverLabel.SelectedValue = m_strSelectIDValue
                dtLastReport.Text = String.Empty
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Dim Logmessage As New System.Text.StringBuilder
            Logmessage.Append("There is error in loadfunddetails of form addeditfund.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
            Logmessage = Nothing

        Finally
            dr = Nothing
            DataTable = Nothing
            ds = Nothing
        End Try
    End Sub

#End Region

#Region "SetValues"
    Private Sub SetFundValue()
        Dim datatable As New DataTable
        Dim IDR As IDataReader = Nothing
        Try
            IDR = cAddModifyFund.GetFundByFundID(FundId)
            datatable.Load(IDR)
            mdlCommon.ChangeDataReaderDataTable(datatable)
            If (datatable.Rows.Count = 0) Then
                If mblnIsEditInDataHub Then
                    mblnCancelDataHub = True
                    Me.Close()
                End If
                Exit Sub
            End If
            For i As Integer = 0 To datatable.Rows.Count - 1
                If (IsDBNull(datatable.Rows(i).Item("FundName")) = False) Then
                    txtFundName.Text = datatable.Rows(i).Item("FundName").ToString().Trim()
                Else
                    txtFundName.Text = txtFundName.Text
                End If

                If (IsDBNull(datatable.Rows(i).Item("FundTypeID")) = False) Then
                    cmbFundType.SelectedValue = datatable.Rows(i).Item("FundTypeID").ToString()
                    If (cmbFundType.SelectedValue Is Nothing) Then
                        cmbFundType.SelectedIndex = m_strSelectIDValue
                    End If
                Else
                    cmbFundType.SelectedIndex = m_strSelectIDValue
                End If

                If (IsDBNull(datatable.Rows(i).Item("StyleID")) = False) Then
                    cmbStyle.SelectedValue = datatable.Rows(i).Item("StyleID").ToString()
                    If (cmbStyle.SelectedValue Is Nothing) Then
                        cmbStyle.SelectedIndex = m_strSelectIDValue
                    End If
                    If cmbStyle.SelectedItem Is Nothing Then
                        Dim d As DataRowView
                        Dim str As String = "" ' d.Row.ItemArray.GetValue(0)
                        If (cmbStyle.Text = "") Then
                            For ij As Integer = 0 To cmbStyle.Items.Count - 1
                                d = cmbStyle.Items(ij)
                                If (d.Row.Item("Style").ToString().ToLower = "na") Then
                                    cmbStyle.SelectedIndex = ij
                                    Exit For
                                End If
                            Next
                        End If
                    End If

                Else
                    cmbStyle.SelectedIndex = 0
                End If
                If (IsDBNull(datatable.Rows(i).Item("TurnoverID")) = False) Then
                    SetIndex(cmbTurnoverLabel, datatable.Rows(i).Item("TurnoverID").ToString().Trim(), "TurnoverID")
                    If (cmbTurnoverLabel.SelectedValue Is Nothing) Then
                        cmbTurnoverLabel.SelectedIndex = m_strSelectIDValue
                    Else
                        cmbTurnoverLabel.SelectedValue = datatable.Rows(i).Item("TurnoverID").ToString
                    End If
                    'If (cmbTurnoverLabel.Text.ToLower = "na") Then
                    '    cmbTurnoverLabel.SelectedIndex = m_strSelectIDValue
                    'End If
                Else
                    cmbTurnoverLabel.SelectedIndex = m_strSelectIDValue
                End If


                If (IsDBNull(datatable.Rows(i).Item("CountryID")) = False) Then
                    cmbCountry.SelectedValue = datatable.Rows(i).Item("CountryID").ToString()
                    If (cmbCountry.SelectedValue Is Nothing) Then
                        cmbCountry.SelectedIndex = m_strSelectIDValue
                    End If

                Else
                    cmbCountry.SelectedIndex = m_strSelectIDValue
                End If

                If (IsDBNull(datatable.Rows(i).Item("FndEntityID")) = False) Then
                    lblFundID.Text = datatable.Rows(i).Item("FndEntityID").ToString().Trim()
                    'FundId = lblFundID.Text
                Else
                    FundId = ""
                End If

                If (IsDBNull(datatable.Rows(i).Item("Portfolio Value")) = False) Then
                    txtPValue.Text = datatable.Rows(i).Item("Portfolio Value").ToString().Trim()
                End If

                If (IsDBNull(datatable.Rows(i).Item("P/E Ratio")) = False) Then
                    txtPERatio.Text = datatable.Rows(i).Item("P/E Ratio").ToString().Trim()
                End If

                If (IsDBNull(datatable.Rows(i).Item("P/B Ratio")) = False) Then
                    txtPRRatio.Text = datatable.Rows(i).Item("P/B Ratio").ToString().Trim()
                End If

                If (IsDBNull(datatable.Rows(i).Item("Dividend Yield")) = False) Then
                    txtDYield.Text = datatable.Rows(i).Item("Dividend Yield").ToString().Trim()
                End If

                If (IsDBNull(datatable.Rows(i).Item("Sales Growth")) = False) Then
                    txtSalesGrowth.Text = datatable.Rows(i).Item("Sales Growth").ToString().Trim()
                End If

                If (IsDBNull(datatable.Rows(i).Item("Price Momentum")) = False) Then
                    txtPMomentum.Text = datatable.Rows(i).Item("Price Momentum").ToString().Trim()
                End If

                If (IsDBNull(datatable.Rows(i).Item("Relative Strength")) = False) Then
                    txtRelativeStrength.Text = datatable.Rows(i).Item("Relative Strength").ToString().Trim()
                End If
                If (IsDBNull(datatable.Rows(i).Item("Beta")) = False) Then
                    txtBeta.Text = datatable.Rows(i).Item("Beta").ToString().Trim()
                End If
                If (IsDBNull(datatable.Rows(i).Item("LastReportDate")) = False) Then
                    dtLastReport.Value = datatable.Rows(i).Item("LastReportDate").ToString().Trim()
                    If dtLastReport.Value = "01/01/2001" Then
                        dtLastReport.Value = ""
                    End If
                End If
                If (IsDBNull(datatable.Rows(i).Item("EStatusID")) = False) Then
                    If (EntityStatus.ACTVE = datatable.Rows(i).Item("EStatusID").ToString().Trim()) Then
                        rdActive.Checked = True
                    ElseIf (EntityStatus.DELTE = datatable.Rows(i).Item("EStatusID").ToString().Trim()) Then
                        rdDelete.Checked = True
                    ElseIf (EntityStatus.DRMNT = datatable.Rows(i).Item("EStatusID").ToString().Trim()) Then
                        rdDormant.Checked = True
                    End If
                End If
            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Dim Logmessage As New System.Text.StringBuilder
            Logmessage.Append("There is error in setfundvalue of form addeditfund.")
            Logmessage.Append(Environment.NewLine)
            Logmessage.Append("Error Message - ")
            Logmessage.Append(ex.ToString())
            Me.LogErrorToDatabase(Logmessage.ToString)
            Logmessage = Nothing
        Finally
            IDR = Nothing
        End Try
    End Sub
#End Region

#Region "Events"

    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
        mblnCancelDataHub = True
        Me.Close()
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnSaveInv_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveInv.Click
        ValidationManager.IsButtonClick = True
        If IsValid() Then
            Return
        End If
        If FundId <> 0 Then
            If _LinkedData Is Nothing And _IsInvFundLinked = False Then
                _IsInvFundLinked = False
            End If
            If (CheckContentsChange() = False) Then
                _IsDetailChanged = False
                If _IsInvFundLinked = False Then
                    Me.Close()
                    Return
                End If
            End If
        End If
        If InsertUpdateFund() Then
            If mintProjectID > 0 Then
                If mdtNewFundDetails IsNot Nothing Then
                    If mdtNewFundDetails.Rows.Count > 0 Then
                        InvestorDetailsForDataHub()
                        _IsCurrentlySaved = True
                    End If
                ElseIf mblnIsEditInDataHub Then
                    GetInvestorDetailsForDatahub()
                    Me.Close()
                End If
            End If
            If lblFundID.Visible = False Then
                Me.Close()
            End If
        End If
    End Sub
    Private Sub InvestorDetailsForDataHub()
        Dim ldatarow As DataRow
        Try
            If mdtNewFundDetails IsNot Nothing Then
                If mdtNewFundDetails.Rows.Count > 0 Then
                    mlstFundDetails = cAddModifyInvestor.GetDataHubInvestor(FundId, CommonCLL.UserID, mintProjectID, False)
                    Exit Sub
                End If
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, "Add Edit Fund", mintProjectID)
        Finally
            ldatarow = Nothing
        End Try
    End Sub

    Private Sub GetInvestorDetailsForDatahub()
        mlstFundDetails = New List(Of AddNewInvestor) '= cAddModifyInvestor.GetDataHubInvestor(InvestorId, CommonCLL.UserID, mintProjectID, True)
        Dim lclsAddNewInvestor As New AddNewInvestor
        lclsAddNewInvestor.Country = cmbCountry.Text
        lclsAddNewInvestor.FundID = FundId
        lclsAddNewInvestor.ID = lblFundID.Text
        lclsAddNewInvestor.FundName = txtFundName.Text
        lclsAddNewInvestor.InvStatusCode = GetCheckedStatus()
        lclsAddNewInvestor.IsInvestor = False
        lclsAddNewInvestor.ProjectId = mintProjectID
        lclsAddNewInvestor.Style = cmbStyle.Text
        lclsAddNewInvestor.Turnover = cmbTurnoverLabel.Text
        lclsAddNewInvestor.Type = cmbFundType.Text
        lclsAddNewInvestor.Status = GetStatusValue()
        lclsAddNewInvestor.MInvestorTypeID = cmbFundType.SelectedValue
        mlstFundDetails.Add(lclsAddNewInvestor)
    End Sub

    Private Function GetStatusValue() As String
        If rdActive.Checked Then
            Return rdActive.Text
        ElseIf rdDelete.Checked Then
            Return rdActive.Text
        ElseIf rdDormant.Checked Then
            Return rdActive.Text
        End If
    End Function

    Private Function GetCheckedStatus() As String
        Dim str As String = String.Empty
        If rdActive.Checked Then
            str = EntityStatus.ACTVE.ToString
        ElseIf rdDelete.Checked Then
            str = EntityStatus.DELTE.ToString
        ElseIf rdDormant.Checked Then
            str = EntityStatus.DRMNT.ToString
        End If
        Return str
    End Function

    Private Sub rdDelete_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdDelete.CheckedChanged
        If _IsLoading = False Then
            If rdDelete.Checked Then
                ShowMessageInfo(MessageManager.GetMessage("ONEMARKEDDELETE"))
            End If
        End If
    End Sub

    Private Sub grdInvestors_BeforeEnterEditMode(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles grdInvestors.BeforeEnterEditMode
        grdInvestors.ActiveRow.Activation = Activation.ActivateOnly
    End Sub

    Private Sub grdInvestors_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles grdInvestors.MouseClick
        Dim grid As UltraGrid = sender
        Dim controlelement As Infragistics.Win.UIElement = grid.DisplayLayout.UIElement
        Dim elementAtPoint As Infragistics.Win.UIElement = If(controlelement IsNot Nothing, controlelement.ElementFromPoint(e.Location), Nothing)
        Dim column As UltraGridColumn = Nothing
        While elementAtPoint IsNot Nothing
            Dim headerElement As HeaderUIElement = TryCast(elementAtPoint, HeaderUIElement)
            If headerElement IsNot Nothing AndAlso TypeOf headerElement.Header Is Infragistics.Win.UltraWinGrid.ColumnHeader Then
                column = TryCast(headerElement.GetContext(GetType(UltraGridColumn)), UltraGridColumn)
                Exit While
            End If
            elementAtPoint = elementAtPoint.Parent
        End While
        If column IsNot Nothing Then
            Dim data As DataTable = grdInvestors.DataSource
            Dim investorLink As New AddInvestorLink
            investorLink.dtLinked = data
            investorLink.Text = "Investor Fund Link"
            investorLink.ShowDialog()
            If investorLink.dtLinked IsNot Nothing Then
                data.Merge(investorLink.dtLinked, True, MissingSchemaAction.Ignore)
                _PerDataTable.Merge(investorLink.dtLinked, True, MissingSchemaAction.Ignore)
                grdInvestors.DataSource = data
                _LinkedData = investorLink.dtLinked
                _IsInvFundLinked = True
            End If
            investorLink = Nothing
        End If
    End Sub

    Private Sub txtBeta_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Try
            Dim st As String
            If Clipboard.ContainsText() Then
                st = Clipboard.GetText()
                If (Validations.CheckNumericString(st) = False) Then
                    e.Handled = True
                    Clipboard.Clear()
                Else
                    e.Handled = False
                End If
            Else
                '  e.Handled = False
                If (Validations.CheckNumeric(e.KeyChar) = False) Then
                    e.Handled = True
                Else
                    e.Handled = False
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            ' nonNumberEntered = False
        End Try
    End Sub

    Private Sub txtDYield_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Try
            Dim st As String
            If Clipboard.ContainsText() Then
                st = Clipboard.GetText()
                If (Validations.CheckNumericString(st) = False) Then
                    e.Handled = True
                    Clipboard.Clear()
                Else
                    e.Handled = False
                End If
            Else
                '  e.Handled = False
                If (Validations.CheckNumeric(e.KeyChar) = False) Then
                    e.Handled = True
                Else
                    e.Handled = False
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            ' nonNumberEntered = False
        End Try
    End Sub

    Private Sub txtPERatio_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Try
            Dim st As String
            If e.KeyChar = "." Then
                isDecimal = True
            Else
                isDecimal = False
            End If
            If Clipboard.ContainsText() Then
                st = Clipboard.GetText()
                If (Validations.CheckNumericString(st) = False) Then
                    e.Handled = True
                    Clipboard.Clear()
                Else
                    e.Handled = False
                End If
            Else
                '  e.Handled = False
                If (Validations.CheckNumeric(e.KeyChar) = False) Then
                    e.Handled = True
                Else
                    e.Handled = False
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            ' nonNumberEntered = False
        End Try
    End Sub

    Private Sub txtPMomentum_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Try
            Dim st As String
            If Clipboard.ContainsText() Then
                st = Clipboard.GetText()
                If (Validations.CheckNumericString(st) = False) Then
                    e.Handled = True
                    Clipboard.Clear()
                Else
                    e.Handled = False
                End If
            Else
                '  e.Handled = False
                If (Validations.CheckNumeric(e.KeyChar) = False) Then
                    e.Handled = True
                Else
                    e.Handled = False
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            ' nonNumberEntered = False
        End Try
    End Sub

    Private Sub txtPRRatio_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Try
            Dim st As String
            If Clipboard.ContainsText() Then
                st = Clipboard.GetText()
                If (Validations.CheckNumericString(st) = False) Then
                    e.Handled = True
                    Clipboard.Clear()
                Else
                    e.Handled = False
                End If
            Else
                '  e.Handled = False
                If (Validations.CheckNumeric(e.KeyChar) = False) Then
                    e.Handled = True
                Else
                    e.Handled = False
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            ' nonNumberEntered = False
        End Try
    End Sub

    Private Sub txtPValue_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Try
            Dim st As String
            If e.KeyChar = "." Then
                isDecimal = True
            Else
                isDecimal = False
            End If
            If Clipboard.ContainsText() Then
                st = Clipboard.GetText()
                If (Validations.CheckNumericString(st) = False) Then
                    e.Handled = True
                    Clipboard.Clear()
                Else
                    e.Handled = False
                End If
            Else
                '  e.Handled = False
                If (Validations.CheckNumeric(e.KeyChar) = False) Then
                    e.Handled = True
                Else
                    e.Handled = False
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            ' nonNumberEntered = False
        End Try
    End Sub

    Private Sub txtRelativeStrength_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Try
            Dim st As String
            If Clipboard.ContainsText() Then
                st = Clipboard.GetText()
                If (Validations.CheckNumericString(st) = False) Then
                    e.Handled = True
                    Clipboard.Clear()
                Else
                    e.Handled = False
                End If
            Else
                '  e.Handled = False
                If (Validations.CheckNumeric(e.KeyChar) = False) Then
                    e.Handled = True
                Else
                    e.Handled = False
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            ' nonNumberEntered = False
        End Try
    End Sub

    Private Sub txtSalesGrowth_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Try
            Dim st As String
            If Clipboard.ContainsText() Then
                st = Clipboard.GetText()
                If (Validations.CheckNumericString(st) = False) Then
                    e.Handled = True
                    Clipboard.Clear()
                Else
                    e.Handled = False
                End If
            Else
                '  e.Handled = False
                If (Validations.CheckNumeric(e.KeyChar) = False) Then
                    e.Handled = True
                Else
                    e.Handled = False
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            ' nonNumberEntered = False
        End Try
    End Sub

#End Region

#Region "Value Changed"
    Private Sub txtPValue_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If txtPValue.Text = String.Empty Then Exit Sub
        If isDecimal Then Exit Sub
        If CheckValueChange = False Then
            Dim value As Double
            Dim index As Integer = txtPValue.SelectionStart
            Dim stringValue As String = txtPValue.Text
            stringValue = stringValue.Replace(",", "")
            Dim isbool As Boolean = False
            isbool = Double.TryParse(stringValue, value)
            If isbool Then
                If Not stringValue.Contains(".") Then
                    stringValue = value.ToString("#,##0")
                Else
                    stringValue = value.ToString("#,##0.000")
                End If
            End If
            CheckValueChange = True
            txtPValue.Text = stringValue
            CheckValueChange = False
            'txtSharesIssue.Update()
            txtPValue.SelectionStart = index
        End If
    End Sub

    Private Sub txtPRRatio_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If txtPRRatio.Text = String.Empty Then Exit Sub
        If CheckValueChange = False Then
            Dim value As Int64
            Dim stringValue As String = txtPRRatio.Text
            stringValue = stringValue.Replace(",", "")
            Dim isbool As Boolean = False
            isbool = Int64.TryParse(stringValue, value)
            If isbool Then
                stringValue = value.ToString("#,##0")
            End If
            CheckValueChange = True
            txtPRRatio.Text = stringValue
            CheckValueChange = False
            'txtSharesIssue.Update()
            txtPValue.SelectionStart = txtPValue.Text.Length
        End If
    End Sub

    Private Sub txtPERatio_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If txtPERatio.Text = String.Empty Then Exit Sub
        If CheckValueChange = False Then
            Dim index As Integer = txtPERatio.SelectionStart
            Dim value As Double
            Dim stringValue As String = txtPERatio.Text
            Dim s As String = String.Format("{0:#,##0.00}", value)
            'stringValue = stringValue.Format("#,##0.000") '.Replace(",", "")
            Dim isbool As Boolean = False
            isbool = Double.TryParse(stringValue, value)
            If isbool Then
                If Not stringValue.Contains(".") Then
                    stringValue = value.ToString("#,##0")
                Else
                    stringValue = value.ToString("#,##0.000")
                End If
            End If
            CheckValueChange = True
            txtPERatio.Text = stringValue
            CheckValueChange = False
            'txtSharesIssue.Update()
            txtPERatio.SelectionStart = index
        End If
    End Sub

    Private Sub txtPMomentum_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If txtPMomentum.Text = String.Empty Then Exit Sub
        If CheckValueChange = False Then
            Dim value As Int64
            Dim stringValue As String = txtPMomentum.Text
            stringValue = stringValue.Replace(",", "")
            Dim isbool As Boolean = False
            isbool = Int64.TryParse(stringValue, value)
            If isbool Then
                stringValue = value.ToString("#,##0")
            End If
            CheckValueChange = True
            txtPMomentum.Text = stringValue
            CheckValueChange = False
            'txtSharesIssue.Update()
            txtPMomentum.SelectionStart = txtPMomentum.Text.Length
        End If
    End Sub

    Private Sub txtSalesGrowth_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If txtSalesGrowth.Text = String.Empty Then Exit Sub
        If CheckValueChange = False Then
            Dim value As Int64
            Dim stringValue As String = txtSalesGrowth.Text
            stringValue = stringValue.Replace(",", "")
            Dim isbool As Boolean = False
            isbool = Int64.TryParse(stringValue, value)
            If isbool Then
                stringValue = value.ToString("#,##0")
            End If
            CheckValueChange = True
            txtSalesGrowth.Text = stringValue
            CheckValueChange = False
            'txtSharesIssue.Update()
            txtSalesGrowth.SelectionStart = txtSalesGrowth.Text.Length
        End If
    End Sub

    Private Sub txtRelativeStrength_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If txtRelativeStrength.Text = String.Empty Then Exit Sub
        If CheckValueChange = False Then
            Dim value As Int64
            Dim stringValue As String = txtRelativeStrength.Text
            stringValue = stringValue.Replace(",", "")
            Dim isbool As Boolean = False
            isbool = Int64.TryParse(stringValue, value)
            If isbool Then
                stringValue = value.ToString("#,##0")
            End If
            CheckValueChange = True
            txtRelativeStrength.Text = stringValue
            CheckValueChange = False
            'txtSharesIssue.Update()
            txtRelativeStrength.SelectionStart = txtRelativeStrength.Text.Length
        End If
    End Sub

    Private Sub txtBeta_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If txtBeta.Text = String.Empty Then Exit Sub
        If CheckValueChange = False Then
            Dim value As Int64
            Dim stringValue As String = txtBeta.Text
            stringValue = stringValue.Replace(",", "")
            Dim isbool As Boolean = False
            isbool = Int64.TryParse(stringValue, value)
            If isbool Then
                stringValue = value.ToString("#,##0")
            End If
            CheckValueChange = True
            txtBeta.Text = stringValue
            CheckValueChange = False
            txtBeta.SelectionStart = txtBeta.Text.Length
        End If
    End Sub

    Private Sub txtDYield_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If txtDYield.Text = String.Empty Then Exit Sub
        If CheckValueChange = False Then
            Dim value As Int64
            Dim stringValue As String = txtDYield.Text
            stringValue = stringValue.Replace(",", "")
            Dim isbool As Boolean = False
            isbool = Int64.TryParse(stringValue, value)
            If isbool Then
                stringValue = value.ToString("#,##0")
            End If
            CheckValueChange = True
            txtDYield.Text = stringValue
            CheckValueChange = False
            txtDYield.SelectionStart = txtDYield.Text.Length
        End If
    End Sub
#End Region


End Class